﻿
using Sorting.App.Models;


using Sorting.App.Services;
using Flurl.Http;
using Flurl;
using Modules.Units;

namespace Sorting.App.Services
{


    /// <summary>
    /// 登录服务
    /// </summary>
    public class LoginService
    {

        private ServiceApis serviceApis = new ServiceApis();

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="username"></param>
        /// <param name="mD5_Pwd"></param>
        /// <returns></returns>
        public async Task<ResfulModel<UserEntity>> Login(string username, string pwd)
        {


            return await serviceApis.LoginApi
                .SetQueryParams(new
                {
                    username = username,
                    pwd = pwd
                }).PostAsync()
                .ReceiveJson<ResfulModel<UserEntity>>();

        }



        /// <summary>
        /// 获取全部信息
        /// </summary>
        /// <param name="username"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public async Task<ResfulModel<List<UserEntity>>> GetUserEntities()
        {
            var  res= await serviceApis.GetUserEntitiesApi
               .WithOAuthBearerToken(AppsettingUnits.Token)
               .PostAsync()
               .ReceiveJson<ResfulModel<List<UserEntity>>>();

            return res;

        }



        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="username"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public async Task<ResfulModel<bool>> AddUser( UserEntity userEntity)
        {
            var res = await serviceApis.AddUserApi
               .WithOAuthBearerToken(AppsettingUnits.Token)
              .PostJsonAsync(userEntity)
               .ReceiveJson<ResfulModel<bool>>();

            
            return res;

        }





        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="username"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public async Task<ResfulModel<bool>> EditUser(UserEntity userEntity)
        {
            var res = await serviceApis.EditUserApi
               .WithOAuthBearerToken(AppsettingUnits.Token)
       .PostJsonAsync(userEntity)
               .ReceiveJson<ResfulModel<bool>>();

            return res;

        }


         
        /// <summary>
        /// 更新密码
        /// </summary>
        /// <param name="username"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public async Task<ResfulModel<dynamic>> UpdatePwd(string username, string oldpwd, string newpwd)
        {
            var res = await serviceApis.UpdatePwdApi
               .WithOAuthBearerToken(AppsettingUnits.Token)
               .SetQueryParams(new
               {

                   username = username,
                   oldpwd = oldpwd,
                   newpwd = newpwd
               })
               .PostAsync()
               .ReceiveJson<ResfulModel<dynamic>>();

            return res;

        }

    }

}
